﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;

using AesApplication.Models;

namespace AesApplication.Controllers
{
    public class HomeController : Controller
    {
        private AesContext db = new AesContext();

        public ActionResult Index()
        {
            return View(db.Stores.ToList());
        }

        public ActionResult About()
        {
            return View("About");
        }

        public ActionResult Contact()
        {
            return View();
        }   

        //
        // GET: /Home/Jobs/5
        // Takes Store ID, shows jobs for that store organized by department

        public ActionResult Jobs(int id = 0)
        {
            JobsViewModel model = new JobsViewModel();
            model.Stores = from store in db.Stores where (store.ID != id) select store;
            model.Selected = db.Stores.Find(id);

            if (model.Selected == null)
            {
                return HttpNotFound();
            }
            return View(model);
        }

        //
        // GET: /Home/Job/5
        // Takes Job ID, shows job details and Apply link

        public ActionResult Job(int id = 0)
        {
            Job job = db.Jobs.Find(id);
            if (job == null)
            {
                return HttpNotFound();
            }
            return View(job);
        }

        //
        // GET: /Home/Apply/5
        // Takes Job ID, shows Application form for job

        public ActionResult Apply(int id = 0)
        {
            ApplyViewModel model = new ApplyViewModel();

            model.Job = db.Jobs.Find(id);
            if (model.Job == null)
            {
                return HttpNotFound();
            }

            if (model.Job.Questionnaire != null)
            {
                model.Questions = model.Job.Questionnaire.Questions.OrderBy(c => c.Number).ToList();
            }

            return View(model);
        }

        // Performs auto-screening on an Application, returns whether screening passed
        private bool AutoScreen(Application app)
        {
            bool success = true;

            foreach (Answer answer in app.Answers)
            {
                if (answer.Question.AutoScreen && answer.Question.ExpectedAnswer != null)
                {
                    string expected = answer.Question.ExpectedAnswer.Trim();
                    if (expected.Length != 0)
                    {
                        // TODO: Implement numerical answer comparison
                        // (currently, this is just a simple case-insensitive string comparison)
                        bool match = String.Equals(answer.Text.Trim(), expected,
                            StringComparison.OrdinalIgnoreCase);
                        if (!match)
                        {
                            success = false;
                            break;
                        }
                    }
                }
            }

            return success;
        }

        //
        // POST: /Home/Apply/5
        // Submits an Application for a job

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Apply(int id, ApplyViewModel model)
        {
            model.Job = db.Jobs.Find(id);
            if (model.Job == null)
            {
                return HttpNotFound();
            }

            if (model.Job.Questionnaire != null)
            {
                model.Questions = model.Job.Questionnaire.Questions.OrderBy(c => c.Number).ToList();
            }

            if (ModelState.IsValid)
            {
                // Save Application and Answers to database

                model.Application.Job = model.Job;
                db.Applications.Add(model.Application);

                if (model.Job.Questionnaire != null)
                {
                    for (int i = 0; i < model.Questions.Count; ++i)
                    {
                        // FIXME: Crash when submitting application with unanswered questions
                        Question question = model.Questions[i];
                        Answer answer = model.Answers[i];

                        answer.Question = question;
                        answer.Application = model.Application;
                        db.Answers.Add(answer);
                    }
                }

                bool passed = AutoScreen(model.Application);

                model.Application.Status = passed ? ApplicationStatus.AwaitingHumanScreening
                    : ApplicationStatus.AutoRejected;

                db.SaveChanges();

                if (passed)
                {
                    return RedirectToAction("Success");
                }
                else
                {
                    return RedirectToAction("Failure");
                }
            }

            return View(model);
        }

        //
        // GET: /Home/Success
        // Displayed after submitting an application that passes auto-screening

        public ActionResult Success()
        {
            return View();
        }

        //
        // GET: /Home/Failure
        // Displayed after submitting an application that fails auto-screening

        public ActionResult Failure()
        {
            return View();
        }

        //
        // GET: /Home/Application/5
        // Displays an Application

        public ActionResult Application(int id = 0)
        {
            Application model = db.Applications.Find(id);
            if (model == null)
            {
                return HttpNotFound();
            }

            return View(model);
        }
    }
}
